package com.java.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java.dao.ActivityUserDao;
import com.java.domain.ActivityUser;
import com.java.domain.event.ActivityUserEvent;
import com.java.service.UserService;
import com.java.util.MyCons;
import com.java.util.MyTools;
import com.java.util.interceptor.LoginUser;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@Service
public class UserServiceImpl implements UserService {
	
	@Resource private ActivityUserDao userDao;

	@Override
	public BaseRespone login(String account, String password) {
		ActivityUserEvent info = userDao.selectByInfo(new ActivityUser().setUserAccount(account));
		if(null == info) {
			return BaseRespone.error("Account error");
		}
		if(!MyTools.md5Match(password, info.getUserPassword())) {
			return BaseRespone.error("Password error");
		}
		if(info.getUserStatus() == 0) {
			return BaseRespone.error("Un Available User");
		}
		LoginUser user = new LoginUser(info.getUserId(), account, info.getUserName(), info.getUserAvatar(), info);
		ServletUtil.getSession().setAttribute(MyCons.USER_ROLE, user);
		return BaseRespone.success("Success");
	}

	@Override
	public BaseRespone register(String account, String password, Integer roleType, String name, String mobile) {
		ActivityUserEvent info = userDao.selectByInfo(new ActivityUser().setUserAccount(account));
		if(null != info) {
			return BaseRespone.error("Account already exists");
		}
		ActivityUser user = new ActivityUser();
		user.setUserId(MyTools.getUUID())
		.setUserAccount(account)
		.setUserPassword(MyTools.getMd5(password))
		.setUserType(roleType)
		.setUserName(name)
		.setUserMobile(mobile)
		.setUserAvatar("/img/head.png")
		.setUserStatus(1)
		.setUserTime(new Date());
		int row = userDao.insertSelective(user);
		return BaseRespone.result(row, "Register Success", "Register Fail");
	}

	@Override
	public ActivityUserEvent getUserById(String userId) {
		ActivityUserEvent user = userDao.selectByKey(userId);
		return user;
	}

	@Override
	public TableRespone getList(int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		List<ActivityUserEvent> list = userDao.selectAll(params, page, size);
		long total = userDao.selectAllCount(params);
		return new TableRespone(list, total);
	}

	@Override
	public BaseRespone updateUser(ActivityUser user) {
		ActivityUser info = userDao.selectByKey(user.getUserId());
		info.setUserName(user.getUserName())
		.setUserMobile(user.getUserMobile())
		.setUserIdcard(user.getUserIdcard())
		.setUserAvatar(user.getUserAvatar());
		int row = userDao.updateSelective(info);
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone updatePassword(String userId, String password) {
		ActivityUser info = userDao.selectByKey(userId);
		info.setUserPassword(password);
		int row = userDao.updateSelective(info);
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone unAvailableUser(String userId) {
		ActivityUser user = userDao.selectByKey(userId);
		user.setUserStatus(0);
		int row = userDao.updateSelective(user);
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone availableUser(String userId) {
		ActivityUser user = userDao.selectByKey(userId);
		user.setUserStatus(1);
		int row = userDao.updateSelective(user);
		return BaseRespone.result(row, "Success");
	}

	@Override
	public void delUserForTest(String userAccount) {
		ActivityUserEvent info = userDao.selectByInfo(new ActivityUser().setUserAccount(userAccount));
		userDao.deleteByKey(info.getUserId());
	}

}
